package com.hkfn.rfb;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.bean.ConstData;
import com.hkfn.rfb.common.fragment.BaseFragment;
import com.hkfn.rfb.common.utils.PreferencesUtils;
import com.hkfn.rfb.common.utils.Utils;
import com.hkfn.rfb.databinding.AMainBinding;
import com.hkfn.rfb.rfb.fragment.RfbMainFragment;
import com.hkfn.rfb.service.SaveDataService;
import com.hkfn.rfb.service.SaveDataTimerListener;

public class MainActivity extends BaseActivity {
    private static final String TAG = "xuhan-MainActivity";

    private AMainBinding aMainBinding;

    private BaseFragment baseFragment;

    /**
     * 定时保存数据定期刷新控件发送服务
     */
    private SaveDataService saveDataService;
    //定义服务绑定的回调，传递给bindService()
    private ServiceConnection saveDataConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            //我们已经绑定到MyBinderService，向下转型转换IBinder为MyBinderService.MyServiceBinder并获得MyBinderService实例
            SaveDataService.SaveDataServiceBinder binder = (SaveDataService.SaveDataServiceBinder) service;
            saveDataService = binder.getService();
            saveDataService.setSaveDataTimerListener(new SaveDataTimerListener() {

                @Override
                public void updateViewDataOnTimer(String date) {
                    // 定期刷新控件数据
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != aMainBinding && !date.equals(aMainBinding.includeHeadTitle.tvDate.getText().toString())) {
                                aMainBinding.includeHeadTitle.tvDate.setText(date);
                            }

                            // 每隔5s刷新页面
                            if (null != baseFragment) {
                                baseFragment.updateViewData();
                            }

                        }
                    });

                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            Log.i("ServiceRunningDiscon", "saveDataService");
            stopSaveDataService();
            startSaveDataService();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null == aMainBinding) {
            aMainBinding = AMainBinding.inflate(getLayoutInflater());
            setContentView(aMainBinding.getRoot());
            setImmerseLayout(aMainBinding.includeHeadTitle.getRoot(), aMainBinding.includeHeadTitle.rlHeadContent);
        }

        // 隐藏虚拟按键
        Utils.showOrHideNav(this, true);

        // 记录本次启动时间
        PreferencesUtils.putLong(this, ConstData.START_TIME, System.currentTimeMillis());

        initAction();
    }

    private void initAction() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                initView();

                startSaveDataService();

            }
        });
    }

    private void startSaveDataService() {
        Intent saveDataIntent = new Intent(MainActivity.this, SaveDataService.class);
        bindService(saveDataIntent, saveDataConnection, Context.BIND_AUTO_CREATE);
    }

    private void stopSaveDataService() {
        if (null != saveDataService) {
            unbindService(saveDataConnection);
            saveDataService = null;
        }
    }

    /**
     * 初始化页面
     */
    private void initView() {
        aMainBinding.includeHeadTitle.ivLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.showOrHideNav(MainActivity.this, false);
            }
        });
        aMainBinding.includeHeadTitle.llMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (aMainBinding.includeHeadTitle.tvMenuTitle.getText().toString()) {
                    case ConstData.EMS_MAIN:
                        // 跳转到EMS主页面
                        skipToRfbMainPage();
                        break;
                }
            }
        });
        // 初始显示RFB主页面
        skipToRfbMainPage();
    }

    /**
     * 跳转到Rfb主页面页面
     */
    public void skipToRfbMainPage() {
        // 跳转到RFB主页面
        baseFragment = new RfbMainFragment();
        addFragment(R.id.fl_container, baseFragment);
        aMainBinding.includeHeadTitle.tvTitle.setText(ConstData.RFB_MAIN);
        aMainBinding.includeHeadTitle.tvMenuTitle.setText(ConstData.FUNCTION_MENU);
    }


    /**
     * 设置页面标题
     *
     * @param title
     */
    public void setTitle(String title) {
        aMainBinding.includeHeadTitle.tvTitle.setText(title);
    }

    /**
     * 设置菜单标题
     *
     * @param title
     */
    public void setMenuTitle(String title) {
        aMainBinding.includeHeadTitle.tvMenuTitle.setText(title);
    }

    /**
     * 设置页面内容
     *
     * @param fragment
     */
    public void addFragmentByFra(BaseFragment fragment) {
        baseFragment = fragment;
        addFragment(aMainBinding.flContainer.getId(), fragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        stopSaveDataService();

        aMainBinding = null;
    }




}
